{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "plot += k3d.label('Test dynamic', (0,0,0), mode='dynamic')\n",
    "plot += k3d.label('Test local', (1,0,0), mode='local')\n",
    "plot += k3d.label('Test side 1', (1,1,1), mode='side')\n",
    "plot += k3d.label('Test side 2 with żółć', (0,0,1), mode='side', is_html=True)\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.objects[0].label_box = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Label maker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "X, Y, Z = np.mgrid[:50, :50, :50]\n",
    "\n",
    "scalar_field = ((X-25.0)/20.0)**2 + ((Y-25.0)/20.0)**2 + ((Z-25.0)/20.0)**2 + \\\n",
    "                 np.sin(X * 0.5) * 0.25 + np.cos((Y + Z) * 0.3) * 0.3\n",
    "\n",
    "obj = k3d.marching_cubes(scalar_field.astype(np.float32), level=0.8)\n",
    "plot += obj\n",
    "\n",
    "on_top = widgets.Dropdown(options=[True, False], description='on_top')\n",
    "label_box = widgets.Dropdown(options=[True, False], description='label_box')\n",
    "mode = widgets.Dropdown(options=['dynamic', 'local', 'side'], description='Mode')\n",
    "text = widgets.Text(description='Text', value='{(1,\\\\frac{5}{\\\\pi}, \\\\sqrt{2})}')\n",
    "\n",
    "def add_label(p):\n",
    "    global plot, text, mode, label_box, on_top\n",
    "    plot += k3d.label(text.value, p['position'], mode=mode.value, label_box=label_box.value, on_top=on_top.value)\n",
    "\n",
    "obj.click_callback = add_label\n",
    "\n",
    "display(mode)\n",
    "display(text)\n",
    "display(on_top)\n",
    "display(label_box)\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.mode = 'callback'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Label inspection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "X, Y, Z = np.mgrid[:50, :50, :50]\n",
    "\n",
    "scalar_field = ((X-25.0)/20.0)**2 + ((Y-25.0)/20.0)**2 + ((Z-25.0)/20.0)**2 + \\\n",
    "                 np.sin(X * 0.5) * 0.25 + np.cos((Y + Z) * 0.3) * 0.3\n",
    "\n",
    "obj = k3d.marching_cubes(scalar_field.astype(np.float32), level=0.8)\n",
    "label = k3d.label('', mode='local', color=0x0)\n",
    "\n",
    "plot += obj\n",
    "plot += label\n",
    "\n",
    "def update_label(p):\n",
    "    global label\n",
    "    label.text = '\\\\vec{n} = \\\\begin{bmatrix} ' + \"\\\\\\\\\".join([str(v) for v in p['normal']]) +' \\\\end{bmatrix}'\n",
    "    label.position = p['position']\n",
    "\n",
    "obj.hover_callback = update_label\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.mode = 'callback'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multipile labels in single object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 20\n",
    "positions = np.random.randn(N,3).astype(np.float32)\n",
    "\n",
    "plot2 = k3d.plot()\n",
    "obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='dynamic')\n",
    "# obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='local')\n",
    "# obj = k3d.label([\"lorem ipsum\"] * N, positions, is_html=True, mode='side')\n",
    "plot2 += obj\n",
    "plot2.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.text= list(map(lambda a: str(a), list(range(N))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.position = np.random.randn(N,3).astype(np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
